package cac.fenjian.resource.replacementaosorties.rest;

import cac.fenjian.resource.replacementaosorties.dao.ReplacementAoSortiesDao;
import cac.fenjian.resource.replacementaosorties.service.ReplacementAoSortiesService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/replacementAoSortiesRest")
public class ReplacementAoSortiesRest {
    @Autowired
    private ReplacementAoSortiesService replacementAoSortiesService;

    @Autowired
    private ReplacementAoSortiesDao replacementAoSortiesDao;

    @Autowired
    private PlatformTransactionManager platformTransactionManager;
    @Autowired
    private TransactionDefinition transactionDefinition;
    //查询已分拣，未配送，需拆包得ao
    @PostMapping("searchAo")
    public HashMap searchAo(@RequestBody HashMap map) {
        HashMap hashMap = new HashMap();
        HashMap pageParameter = (HashMap) map.get("pageParameter");
        HashMap searchParams = (HashMap) map.get("searchParams");
        Integer pageNum = (Integer) pageParameter.get("page");
        Integer pageSize = (Integer) pageParameter.get("rows");
        PageHelper.startPage(pageNum, pageSize);
        List<HashMap> lackPackage = replacementAoSortiesDao.searchAo(searchParams);
        PageInfo pageInfo = new PageInfo(lackPackage);
        hashMap.put("retCode", "200");
        hashMap.put("retData", lackPackage);
        hashMap.put("totalCount", pageInfo.getTotal());
        hashMap.put("totalPage", pageInfo.getPages());
        return hashMap;
    }

    //查询已分拣，未配送，需拆包得ao
    @PostMapping("searchMatching")
    public HashMap searchMatching(@RequestBody HashMap map) {
        HashMap hashMap = new HashMap();
        HashMap pageParameter = (HashMap) map.get("pageParameter");
        HashMap searchParams = (HashMap) map.get("searchParams");
        Integer pageNum = (Integer) pageParameter.get("page");
        Integer pageSize = (Integer) pageParameter.get("rows");
        PageHelper.startPage(pageNum, pageSize);
        List<HashMap> lackPackage = replacementAoSortiesDao.searchMatching(searchParams);
        PageInfo pageInfo = new PageInfo(lackPackage);
        hashMap.put("retCode", "200");
        hashMap.put("retData", lackPackage);
        hashMap.put("totalCount", pageInfo.getTotal());
        hashMap.put("totalPage", pageInfo.getPages());
        return hashMap;
    }

    /**
     * AO架次替换
     *
     * @param map
     * @return
     */
    @PostMapping("aoStartPlaneNo")
    public HashMap aoStartPlaneNo(@RequestBody HashMap map) {
        HashMap sourceAoMap = (HashMap) map.get("sourceAoMap");
        String replaceStartPlaneNo = (String)map.get("startPlaneNo");
        return replacementAoSortiesService.aoStartPlaneNo(sourceAoMap,replaceStartPlaneNo);
    }
}
